/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.solarchallenge.server.servlets.gwtservices;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.solarchallenge.client.rpc.OAuthProviderService;
import com.google.solarchallenge.server.enums.OAuthProviderEnum;

import java.util.ArrayList;

/**
 * Server side implementation for GWT RPC {@link OAuthProviderService}.
 *
 * @author Arjun Satyapal
 */
public class OAuthProviderServiceImpl extends RemoteServiceServlet implements
    OAuthProviderService {
  @Override
  public ArrayList<String> getOAuthProviderList() {
    ArrayList<String> list = new ArrayList<String>();
    UserService userService = UserServiceFactory.getUserService();
    if (!userService.isUserAdmin()) {
      // TODO : This is temporary hack. Ideally UnAuthorizedException should
      // be thrown and handled at client side.
      list.add("Unauthorized.");
      return list;
    }

    for (OAuthProviderEnum currEnum : OAuthProviderEnum.values()) {
      list.add(currEnum.name());
    }
    return list;
  }
}
